//
//  ViewController.m
//  05_UIScrollView
//
//  Created by 徐昊 on 16/7/20.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"

// 宏定义 命名两种方式(全称: 预编译指令)
#define kScreenBounds [UIScreen mainScreen].bounds
// 一. 全大写
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
// 二. k + 驼峰命名
#define kWidth kScreenBounds.size.width
#define kHeight kScreenBounds.size.height
@interface ViewController ()<UIScrollViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:kScreenBounds];
    scrollView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:scrollView];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(10, 20, kWidth - 20, 100);
    button.backgroundColor = [UIColor redColor];
    [scrollView addSubview:button];
    // 1.默认 CGSizeZero = CGSizeMake(0, 0)
    // contenSize 设置可以滚动的范围
    scrollView.contentSize = CGSizeMake(1000, 1000);
    // 2.默认 CGPointZero
    // 偏移量 滚动的位置
    // 不设置contentSize, contentOffset也可以改变
    // 问题: 改变contentOffset 什么变了?
    // 答案: 改变contentOffset, scrollView的bounds有了变化
    NSLog(@"scrollView frame is : %@", NSStringFromCGRect(scrollView.frame));
    NSLog(@"scrollView bounds is : %@", NSStringFromCGRect(scrollView.bounds));
    scrollView.contentOffset = CGPointMake(100, 10);
    NSLog(@"scrollView frame is : %@", NSStringFromCGRect(scrollView.frame));
    NSLog(@"scrollView bounds is : %@", NSStringFromCGRect(scrollView.bounds));
    // 3.contentInset
    // 添加一个滚动区域四周的滚动范围
    scrollView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
    // 4.directionalLockEnabled
    // 锁定垂直或水品滚动(45°角, 锁会失效)
    scrollView.directionalLockEnabled = YES;
    // 5.bounces 回弹效果(默认 YES)
//    scrollView.bounces = NO;
    // 6.在没有垂直方向的滚动范围时, 保证垂直方向的回弹效果存在
    scrollView.alwaysBounceVertical = YES;
    // 7.在没有水平方向的滚动范围时, 保证水平方向的回弹效果存在
    scrollView.alwaysBounceHorizontal = YES;
    // 8.pagingEnabled 按页滚动
    scrollView.pagingEnabled = YES;
    // 9.scrollEnabled 滚动视图能否滚动, 默认 YES(可以滚), NO(不能滚)
    scrollView.scrollEnabled = YES;
    // 10.是否显示垂直/水平的滚动指示器, 默认YES
    scrollView.showsHorizontalScrollIndicator = YES;
    scrollView.showsVerticalScrollIndicator = YES;
    // 11.滚动指示器的边距
    scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(100, 0, 0, 0);
    // 12.改变滑动指示器的格式
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    // 改变偏移量(带动画效果)
//    scrollView setContentOffset:<#(CGPoint)#> animated:<#(BOOL)#>
    // 13.
    // tracking 用户是否开始拖动
//    scrollView.tracking;
    // dragging 用户是否在拖动
//    scrollView.dragging
    // decelerating 是否减速中
//    scrollView.decelerating
    // 14.滚动视图 双击状态栏手势是否存在
    scrollView.scrollsToTop = NO;
    // 15.
    scrollView.delegate = self;
    // 16.键盘消失模式
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
    
    // 测试:
    UITextField *textFeild = [[UITextField alloc] initWithFrame:CGRectMake(10, 300, kWidth, 200)];
    textFeild.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:textFeild];
    
    // 17.缩放
    // 如果视图想要缩放, 必须实现viewForZoomingInScrollView:
    scrollView.minimumZoomScale = 0.5;
    scrollView.maximumZoomScale = 2.0;
    
}
// 与其想要缩放的子视图绑定, scrollView的contentSize会跟随这个子视图变化
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return scrollView.subviews.firstObject;
}

// 滚动视图滚动, 就会执行
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // tracking用户触摸
    if (scrollView.tracking) {
        NSLog(@"正在拖动滚动");
    }
    // dragging 用户开始滑动
    if (scrollView.dragging) {
        NSLog(@"用户开始滑动");
    }
    // decelerating 用户触摸结束
    if (scrollView.decelerating) {
        NSLog(@"");
    }
}
// 将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
//    NSLog(@"将要开始拖拽");
}
// 已经结束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
//    NSLog(@"已经结束拖拽");
}
// 将要开始进行减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
//    NSLog(@"将要开始减速");
}
// 已经结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//    NSLog(@"已经结束减速");
}
//
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
